package com.atguigu.gulimall.product.web;

import com.atguigu.gulimall.product.entity.CategoryEntity;
import com.atguigu.gulimall.product.service.CategoryService;
import com.atguigu.gulimall.product.vo.Catelog2Vo;
import org.redisson.api.RCountDownLatch;
import org.redisson.api.RLock;
import org.redisson.api.RSemaphore;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Controller
public class IndexController {

    @Autowired
    RedissonClient redisson;

    @Autowired
    CategoryService categoryService;

    @GetMapping({"/", "/index.html"})
    public String indexPage(Model model){
        // TODO 查询出所有的一级分类
        List<CategoryEntity> categoryEntities = categoryService.getLevel1Categorys();
        // 视图解析器进行拼串
        model.addAttribute("categorys", categoryEntities);
        return "index";
    }

    //index/catalog.json
    @GetMapping("/index/catalog.json")
    @ResponseBody
    public Map<String, List<Catelog2Vo>> getCalalogJson(){
        return categoryService.getCalalogJson();
    }

    @GetMapping("/hello")
    @ResponseBody
    public String hello(){
        // 1、获取一把锁，只要锁的名字一样就是同一把锁
        RLock lock = redisson.getLock("my-lock");
        // redisson解决了两个问题：
        // 1、锁的自动续期：业务超长，锁会自动续期
        // 2、加锁的业务只要运行完成，就不会给当前锁续期，即使不手动解锁，锁默认会在30s以后删除
        lock.lock(10, TimeUnit.SECONDS); // 自动解锁时间一定要大于业务执行时间
        try{
            System.out.println("加锁成功，执行业务"+Thread.currentThread().getId());
            TimeUnit.SECONDS.sleep(30);
        }catch (Exception e){

        }finally {
            System.out.println("解锁成功"+Thread.currentThread().getId());
            lock.unlock();
        }
        return "hello";
    }

    @GetMapping("/lockDoor")
    @ResponseBody
    public String lockDoor(){
        RCountDownLatch lock = redisson.getCountDownLatch("school");
        lock.trySetCount(5);
        try {
            lock.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "lockDoor!放假了";
    }

    @GetMapping("/gogogo/{id}")
    @ResponseBody
    public String gogogo(@PathVariable("id") Long id){
        RCountDownLatch lock = redisson.getCountDownLatch("school");
        lock.countDown();
        return id+"班的人都走了";
    }

    @GetMapping("/park")
    @ResponseBody
    public String park() throws InterruptedException {
        RSemaphore semaphore = redisson.getSemaphore("park");
        semaphore.acquire();
        return "ok";
    }

    @GetMapping("/go")
    @ResponseBody
    public String go(){
        RSemaphore semaphore = redisson.getSemaphore("park");
        semaphore.release();
        return "ok";
    }


}
